{extend name="public/container"}
{block name="content"}
<style type="text/css">
    .avatar-uploader .el-upload {
        width: 350px;
        height: 135px;
    }
    .target-link .el-select {width: 100%;}
</style>
<div id="myApp">
     <div class="douxue-filter padding-20 flex flex-between" style="margin-bottom:20px;">
        <el-form :inline="true" :model="where" size="mini" class="flex">
            <div class="flex flex-between">
              <el-form-item>
                <el-input size="small" v-model="where.rtitle" placeholder="请输入标题">
                     <el-button slot="append" size="small" type="primary" @click="doLoadRows(1)" icon="el-icon-search"></el-button>
                </el-input>
              </el-form-item>
            </div>
        </el-form>
        <div>
            <el-button type="primary" size="mini" @click="doAdd()">新增规则</el-button>
            <el-button type="success" size="mini" @click="goCoupons()">返回代金券</el-button>
        </div>

    </div>

    <div class="padding-10">
        <el-table
            :data="rows"
            style="width: 100%"
            size="mini">
            <el-table-column
                prop="id"
                label="ID"
                width="80"
                align="left">
            </el-table-column>
            <el-table-column
                prop="rtitle"
                label="规则标题"
                width="200"
                align="left">
            </el-table-column>
            <el-table-column
                prop="rtype_display"
                label="规则类型"
                width="200"
                align="left">
            </el-table-column>

            <el-table-column
                prop="rcfg"
                label="规则配置"
                width="300"
                align="left">
                <template slot-scope="scope">
                    <div class="flex" style="align-items: center;" v-for="item in scope.row.rcfg">
                        <div style="color: #666; margin-right: 12px;">{{ item.cdes }}</div>
                        <div class="flex" style="width: 160px; align-items: center;">
                            <template v-if="item.ckey=='route_type'">
                                <el-select size="mini" placeholder="线路类型" v-model="item.cval" :disabled="true">
                                    <el-option label="长线" :value="1"></el-option>
                                    <el-option label="短线" :value="2"></el-option>
                                    <el-option label="周边游" :value="3"></el-option>
                                </el-select>
                            </template>
                        </div>
                    </div>
                </template>
            </el-table-column>

            <el-table-column
                prop="rdesc"
                label="使用说明"
                width="120"
                align="left">
                <template slot-scope="scope">
                    <div v-html="scope.row.rdesc"></div>
                </template>
            </el-table-column>
            <el-table-column
                label=""
                width="90"
                align="left">
                <template slot-scope="scope">
                    <el-link type="primary" size="mini" :underline="false" @click="doAdd(scope.row)">编辑</el-link>
                    <el-link type="danger" size="mini" :underline="false" @click="doDelete(scope.row.id)">删除</el-link>
                </template>
            </el-table-column>
        </el-table>
        <div class="pagination">
            <el-pagination
                small
                :hide-on-single-page="true"
                layout="prev, pager, next"
                :total="rowsCount"
                :page-size="where.limit"
                @current-change="doLoadRows">
            </el-pagination>
        </div>
    </div>
    <!-- 添加/编辑   -->
    <el-dialog title="添加/编辑" v-if="dialogFormVisible" :visible.sync="dialogFormVisible" @close='closeDialog'>
        <el-form :model="formData" :rules="rules" ref="formData" label-width="80px" >
            <el-form-item label="规则名称" prop="rtitle">
                <el-input v-model="formData.rtitle" placeholder="轮播标题" autocomplete="off"></el-input>
            </el-form-item>

			<el-form-item label="适用规则" prop="rtype">
                <el-select
                    size="small"
                    v-model="formData.rtype"
                    placeholder="适用规则"
                    :clearable=true>
                    <el-option label="线路" :value="1"></el-option>
                </el-select>
            </el-form-item>

            <el-form-item label="规则配置">
                <div class="flex" v-for="item in formData.rcfg">
                    <div style="color: #666; margin-right: 12px;">{{ item.cdes }}</div>
                    <div class="flex" style="width: 200px; align-items: center;">
                        <template v-if="item.ckey=='route_type'">
                            <el-select size="small" placeholder="线路类型" v-model="item.cval">
                                <el-option label="长线" :value="1"></el-option>
                                <el-option label="短线" :value="2"></el-option>
                                <el-option label="周边游" :value="3"></el-option>
                            </el-select>
                        </template>
                    </div>
                </div>
            </el-form-item>
			
            <el-form-item label="规则说明">
                <el-input type="textarea" v-model="formData.rdesc" rows="4" :maxlength="150" show-word-limit placeholder="规则说明" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="onSubmit('formData')">确 定</el-button>
                <el-button @click="dialogFormVisible = false">取消</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</div>
{/block}
{block name="script"}
<script>
    var _formData = {
        id:0,
        rtitle: '',
        rtype: 1,
        rcfg: [
            {
                ckey: 'route_type',
                cval: 1,
                cdes: '类型限制'
            }
        ],
        rdesc: ''
    }
    var my = new Vue({
        el: "#myApp",
        data() {
            return{
                dialogFormVisible: false,
                formData:_formData,
                rules:{
                    rtitle: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ],
                    rtype: [
                        { required: true, message: '此项不能为空', trigger: 'blur' },
                    ]
                },
                rowsCount: 0,
                rows: [],
                where:{
                    page:1,
                    limit: pageSize,
                    rtitle:'',
                }
            }
        },
        watch: {

        }
        ,methods: {
            doLoadRows (pn) {
                this.where.page = pn || 1
                axios.get(Url({a:'get_list' , q:this.where})).then(res => {
                    this.rows = res.data.data
                    this.rowsCount = res.data.count
                })
            }
            // 编辑
            ,doAdd ( item ) {
                if ( item != undefined ) {
                    this.formData = item;
                } else {
                    this.formData = _formData
                }
                this.dialogFormVisible = true
            }
            ,onSubmit (formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        axios.post( Url({a:'save'}) , this.formData)
                            .then(res => {
                                this.$message({
                                    message:'操作成功',
                                    type:'success',
                                    duration:1500
                                });
                                this.dialogFormVisible = false;
                                this.doLoadRows(1)
                            }).catch(_ => {
                            this.$message({
                                message: '操作失败',
                                type: 'error'
                            })
                        })
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
            ,doDelete (id) {
                this.$confirm('确认删除该条记录?').then(_ => {
                    axios.get(Url({a:'delete' , q:{id:id}})).then(res => {
                        this.$message({
                            message: res.data.msg,
                            type: res.data.code === 200 ? 'success' : 'error',
                        })
                    }).catch(_ => {
                        this.$message({
                            message: '删除失败',
                            type: 'error'
                        })
                    })

                    return this.doLoadRows();
                }).catch(_ => {})
            }
            // 关闭
            ,closeDialog () {
                this.$refs['formData'].clearValidate();
            }
            ,goCoupons() {
                window.location.href = Url({c:'finance.coupon',a:'index'})
            }
        },
        mounted(){
            this.doLoadRows()
        }
    })
</script>
{/block}